
source('utils.R')
source('read_dataset.R')

donors = list(
  d12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r12_inds = open_dataset(glue('final_long/cycle=2012')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r16_inds = open_dataset(glue('final_long/cycle=2016')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component),
  d20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(d_pcc > 0 | d_sp > 0 | d_jfc > 0) %>% pull(component),
  r20_inds = open_dataset(glue('final_long/cycle=2020')) %>% filter(r_pcc > 0 | r_sp > 0 | r_jfc > 0) %>% pull(component)
)

df12[, nat_quant_col := fct_collapse(nat_quant, "0" = c("0" , "0.1", "0.2" ,"0.3" ,"0.4","0.5", "0.6", "0.7", "0.8"))]
df16[, nat_quant_col := fct_collapse(nat_quant, "0" = c("0" , "0.1", "0.2" ,"0.3" ,"0.4","0.5", "0.6", "0.7", "0.8"))]
df20[, nat_quant_col := fct_collapse(nat_quant, "0" = c("0" , "0.1", "0.2" ,"0.3" ,"0.4","0.5", "0.6", "0.7", "0.8"))]

# a

r12a = df12[component %chin% {
  open_dataset('final_long/cycle=2012') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_other_pcc > 0) %>%
    pull(component)
}]

r12a[, subsq_giver := component %chin% donors[['r12_inds']]]
r12a = r12a[, .(sq = mean(subsq_giver)), nat_quant_col]
r12a[, class := 'Other Pres. Primary \'12 to Romney \'12']

r16a = df16[component %chin% {
  open_dataset('final_long/cycle=2016') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_other_pcc > 0) %>%
    pull(component)
}]

r16a[, subsq_giver := component %chin% donors[['r16_inds']]]
r16a = r16a[, .(sq = mean(subsq_giver)), nat_quant_col]
r16a[, class := 'Other Pres. Primary \'16 to Trump \'16']

r20a = df16[component %chin% {
  open_dataset('final_long/cycle=2020/') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_other_pcc > 0) %>% pull(component)
}]

r20a[, subsq_giver := component %chin% donors[['r20_inds']]]
r20a = r20a[, .(sq = mean(subsq_giver)), nat_quant_col]
r20a[, class := 'Other Pres. Primary \'16 to Trump \'20']

# b

r12b = df12[component %chin% {
  open_dataset('final_long/cycle=2012/') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
    pull(component)
}]

r12b[, subsq_giver := component %chin% {
  open_dataset('final_long/cycle=2016/') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
    pull(component)
}]

r12b = r12b[, .(sq = mean(subsq_giver)), nat_quant_col]
r12b[, class := 'Congressional (R) \'12 to \'16']

r16b = df16[component %chin% {
  open_dataset('final_long/cycle=2016/') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
    pull(component)
}]

r16b[, subsq_giver := component %chin% {
  open_dataset('final_long/cycle=2020/') %>% 
    filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
    pull(component)
}]

r16b = r16b[, .(sq = mean(subsq_giver)), nat_quant_col]
r16b[, class := 'Congressional (R) \'16 to \'20']

r20b = df12[component %chin% {
  open_dataset('final_long/cycle=2012/') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
    pull(component)
}]

r20b[, subsq_giver := component %chin% {
  open_dataset('final_long/cycle=2020/') %>%
    filter(in_l2 == 1, in_cl == 1, total > 0, r_congress > 0) %>%
    pull(component)
}]

r20b = r20b[, .(sq = mean(subsq_giver)), nat_quant_col]
r20b[, class := 'Congressional (R) \'12 to \'20']

save(r12a, r16a, r20a, r12b, r16b, r20b, file = 'summary_data/fig6.rda')
